﻿package net.area80.sitemanager.page
{
	import flash.display.*;
	import flash.events.Event;
	import flash.net.URLRequest;
	import net.area80.Area80;
	import net.area80.sitemanager.events.PageEvent;
	import net.area80.sitemanager.ISiteManager;
	import net.area80.sitemanager.SiteManagerProxy;
	
	/**
	* AbstractPage
	* @author 	Wissarut Pimanmassuriya
	* @mobile	0818338186
	*/
	dynamic public class AbstractPage extends MovieClip implements IPage
	{
		public var siteManager:ISiteManager;
		private var _extras:Object;
		
		public function AbstractPage () {
			if(SiteManagerProxy.hasSiteManager()) siteManager = SiteManagerProxy.siteManager;
			addEventListener(Event.ADDED_TO_STAGE, addToStageHandler, false, 0, true);
			addEventListener(Event.REMOVED_FROM_STAGE, dispose, false, 0, true);
			stop();
		}
		private function dispose (e:Event):void {
			removeEventListener(Event.REMOVED_FROM_STAGE, dispose);
			removeEventListener(Event.ADDED_TO_STAGE, addToStageHandler);
			if(loaderInfo) loaderInfo.removeEventListener(Event.COMPLETE , onLoadedHandler);
		}
		private function addToStageHandler (e:Event):void {
				removeEventListener(Event.ADDED_TO_STAGE, addToStageHandler);
				loaderInfo.addEventListener(Event.COMPLETE , onLoadedHandler, false, 0, true);
		}
		private function onLoadedHandler (e:Event):void {
				loaderInfo.removeEventListener(Event.COMPLETE , onLoadedHandler);
				if (!siteManager) moveIn();
		}
		
		public function get extras ():Object {
				return _extras;
		}
		public function initExtras (extras:Object):void {
				_extras = extras;
		}
		public function moveIn ():void {
				onInit();
		}	
		public function moveOut ():void {
				onOut();
		}
		protected function onInit ():void {
			dispatchEvent(new PageEvent(PageEvent.EVENT_INIT));
		}
		protected function onOut ():void {
			dispatchEvent(new PageEvent(PageEvent.EVENT_OUT));
		}
		/**
		 * Read special variables of current page in string format, ex: "a,b;c,d,e;"
		 * @return special variables of current page
		 */
		public final function getPermaString ():String {
			return (siteManager) ? siteManager.getPermaString() : "";
		}
		/**
		 * Force main swf to load another page 
		 * @param	url	path of page:IPage to load
		 */
		public final function loadPage (url:URLRequest, autoPlay:Boolean = true, onInit:Function = null, onOut:Function = null, extras:Object = null):void {
			if (siteManager) siteManager.loadPage(url, autoPlay, onInit, onOut, extras);
		}
		/**
		 * Freeze stage to unclickable state
		 */
		public final function freeze ():void {
			Area80.log("freeze", this);
			if (siteManager) siteManager.freeze(this);
		}
		/**
		 * Return stage to clickable state
		 */
		public final function unFreeze ():void {
			Area80.log("unFreeze", this);
			if (siteManager) siteManager.unFreeze(this);
		}
	
		/**
		 * Tracking Site Stat
		 * @param	page
		 */
		public final function track (page:String):void {
			Area80.log("track('" + page + "')", this);
			if (siteManager) siteManager.track(page);
		}

	}
	
}